package dataClean;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
 * @author Smartloe
 * @create 2021-05-11 11:37
 */
public class ExcelRead {
    public static void main(String[] args) throws IOException {
        poiExcel("C:\\Users\\25392\\Desktop\\Java基础\\src\\main\\java\\dataClean\\data\\excelread.xlsx");
    }


    public static void poiExcel(String fileName) throws IOException {

        boolean is2007 = true;    //判断是否是excel2007格式
        if (fileName.endsWith("xlsx")) {
            is2007 = false;
        }
        InputStream input = new FileInputStream(fileName);  //建立输入流
        Workbook wb;
        //根据文件格式(2003或者2007)来初始化
        if (is2007) {
            wb = new HSSFWorkbook(input);
        } else {
            wb = new XSSFWorkbook(input);
        }
        //getSheetAt(index:0)表示从wb中获得第一个表格对象。
        Sheet sheet = wb.getSheetAt(0);
//        List<List<Serializable>> lst_total = new ArrayList<List<Serializable>>();
        int lastRowNum = sheet.getLastRowNum();
        for (int i = 0; i <= lastRowNum; i++) {
            //这里很重要,因为在这里定义ArrayList,保证了每次行循环的时候,都是一个空的lst去接当前行的数据.
            List<java.io.Serializable> lst = new ArrayList<java.io.Serializable>();
            //具体到某一行
            Row row = sheet.getRow(i);
            //单元格的数量
            int lastCellNum = row.getLastCellNum();
            for (int j = 0; j < lastCellNum; j++) {
                Cell cell = row.getCell(j);
                //根据cell中的类型来输出数据
                String s =cell.getStringCellValue();
                System.out.println(s);
            }
        }
    }
}


